package com.digitalchina.lxnsy.activity.old;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.digitalchina.lxnsy.R;
import com.digitalchina.lxnsy.api.BusinessAccount;
import com.digitalchina.lxnsy.api.BusinessTrainingVideo;
import com.digitalchina.lxnsy.base.BaseActivity;
import com.digitalchina.lxnsy.constant.MSG;
import com.digitalchina.lxnsy.constant.URL;
import com.digitalchina.lxnsy.entity.ShareFarmListVO;
import com.digitalchina.lxnsy.entity.ShareFarmTypeVO;
import com.digitalchina.lxnsy.entity.TrainingListVO;
import com.digitalchina.lxnsy.entity.VideoOptsVO;
import com.digitalchina.lxnsy.toolkit.CustomDialog;
import com.digitalchina.lxnsy.toolkit.FastJsonUtil;
import com.digitalchina.lxnsy.toolkit.GotoUtil;
import com.digitalchina.lxnsy.toolkit.StringUtil;
import com.digitalchina.lxnsy.toolkit.Utils;
import com.digitalchina.lxnsy.viewholder.TechnologyTrainingVideoListV2Holder;
import com.digitalchina.lxnsy.viewholder.TopTypeVideoHolder;
import com.jude.easyrecyclerview.EasyRecyclerView;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

public class TechnologyTrainingVideoListActivityV2 extends BaseActivity implements RecyclerArrayAdapter.OnLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @Bind(R.id.rv_tab)
    EasyRecyclerView rvTab;
    @Bind(R.id.more)
    ImageView more;
    @Bind(R.id.recycler_view)
    EasyRecyclerView recyclerView;

    private RecyclerArrayAdapter adapter;
    private int page = 1;
    private int limit = 10;
    private List<TrainingListVO> trainingListVO = new ArrayList<>();
    private String keyWords = "";
    private String code = "";

    private RecyclerArrayAdapter adapterH;
    private List<VideoOptsVO> voList;

    private String hotVideo = "";

    @Override
    public void setLayout() {
        setTitle("技术讲堂");
        setContentLayout(R.layout.activity_technology_training_video_list_v2);
        setRightText("搜索");
    }

    @Override
    public void initMember() {
        // 获取视频分类
        BusinessTrainingVideo.getVideoOpts(this, mHandler);

        rvTab.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rvTab.setAdapter(adapterH = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new TopTypeVideoHolder(parent);
            }
        });


        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(Utils.setDividerDecoration(this, 1, 0, 0));
        recyclerView.setAdapterWithProgress(adapter = new RecyclerArrayAdapter(this) {
            @Override
            public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
                return new TechnologyTrainingVideoListV2Holder(parent);
            }
        });

        gridLayoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(2));

        onRefresh();
    }

    @Override
    public void addListener() {
        recyclerView.setRefreshListener(this);
        adapter.setMore(R.layout.view_more, this);
        adapter.setNoMore(R.layout.view_nomore);
        adapter.setError(R.layout.view_error, new RecyclerArrayAdapter.OnErrorListener() {
            @Override
            public void onErrorShow() {
                adapter.resumeMore();
            }

            @Override
            public void onErrorClick() {
                adapter.resumeMore();
            }
        });

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                TrainingListVO item = (TrainingListVO) adapter.getAllData().get(position);


                GotoUtil.gotoActivity(TechnologyTrainingVideoListActivityV2.this, TechnologyTrainingVideoDetailNewActivity.class, "TrainingListVO", item);
            }
        });


        adapterH.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                List<VideoOptsVO> voList = adapterH.getAllData();
                code = voList.get(position).getValue();
                for (int i = 0; i < voList.size(); i++) {
                    voList.get(i).setChecked(i == position);
                }
                adapterH.notifyDataSetChanged();
                if (voList.get(position).getValue().equals("hot"))
                    hotVideo = "hotVideo";
                else
                    hotVideo = "";

                onRefresh();
            }
        });

        more.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                GotoUtil.gotoForResultActivity(TechnologyTrainingVideoListActivityV2.this, ChannelV2SubclassVideoActivity.class, 888);
            }
        });
    }

    @Override
    public void setHandler() {
        mHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);

                if (recyclerView == null) {
                    return;
                }
                if (recyclerView.getSwipeToRefresh().isRefreshing())
                    recyclerView.setRefreshing(false);

                switch (msg.what) {
                    case MSG.MSG_TRAINING_SAVE_SUCCESS:
                        callBack(msg.obj);
                        break;
                    case MSG.MSG_SHARE_FARM_TYPE_SUCCESS:
                        setTopView(msg.obj);
                        break;
                    case MSG.MSG_TRAINING_SAVE_FIELD:
                    case MSG.MSG_SHARE_FARM_TYPE_FIELD:
                        showToast(msg.obj.toString());
                        break;
                }
            }
        };
    }

    /**
     * 设置顶部分类
     *
     * @param obj
     */
    private void setTopView(Object obj) {
        voList = FastJsonUtil.getListBeanRow(obj.toString(), "options", VideoOptsVO.class);

        if (voList == null)
            return;

        List<VideoOptsVO> tempList = new ArrayList<>();
        for (int i = 0; i < voList.size(); i++) {
            if (i == 6)
                break;
            tempList.add(voList.get(i));
        }

        VideoOptsVO vo1 = new VideoOptsVO();
        vo1.setLabel("全部");
        vo1.setValue("");
        tempList.add(0, vo1);
   /*     VideoOptsVO vo2 = new VideoOptsVO();
        vo2.setName("最新");
        vo2.setCode("");
        tempList.add(0, vo2);*/

        for (int i = 0; i < tempList.size(); i++) {
            tempList.get(i).setChecked(i == 0);
        }
        adapterH.addAll(tempList);
    }


    @Override
    public void callBack(Object object) {
        trainingListVO = FastJsonUtil.getListBeanRow(object.toString(), "rows", TrainingListVO.class);

        if (page == 1) { // 刷新
            adapter.clear();
        }

        adapter.addAll(trainingListVO);

        if (trainingListVO != null) {
            if (trainingListVO.size() < limit) {
                adapter.stopMore();
            }
        }
    }

    @Override
    public void onRefresh() {
        page = 1;
        BusinessTrainingVideo.getTrainingVideoList(this, String.valueOf(page), String.valueOf(limit), code, keyWords, "1", mHandler);
    }

    @Override
    public void onLoadMore() {
        if (trainingListVO != null && trainingListVO.size() == limit) {
            page++;
            BusinessTrainingVideo.getTrainingVideoList(this, String.valueOf(page), String.valueOf(limit), code, keyWords, "1", mHandler);
        }

    }

    @Override
    public void rightOnClick() {
        GotoUtil.gotoActivity(TechnologyTrainingVideoListActivityV2.this, TechnologyTrainingVideoSearchActivity.class);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

       /* if (requestCode == 888) {
            if (resultCode == 998) {
                VideoOptsVO vo = (VideoOptsVO) data.getSerializableExtra("selectType");
                code = vo.getCode();
                vo.setSelect(true);
                List<VideoOptsVO> tempList = adapterH.getAllData();
                for (int i = 0; i < tempList.size(); i++) {
                    tempList.get(i).setSelect(false);
                    if (vo.getCode().equals(tempList.get(i).getCode()))
                        tempList.remove(i);
                }
                tempList.add(3, vo);
                adapterH.clear();
                adapterH.addAll(tempList);
                rvTab.scrollToPosition(0);
                onRefresh();
            }

        }*/

    }

}
